package com.zjsru.oneDay;

/**
 * 1920. 基于排列构建数组
 *
 * @Author: cookLee
 * @Date: 2025-05-06
 */
public class BuildArray {

    /**
     * 主
     * \
     * 输入：nums = [0,2,1,5,3,4]
     * 输出：[0,1,2,4,5,3]
     * 解释：数组 ans 构建如下：
     * ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
     * = [nums[0], nums[2], nums[1], nums[5], nums[3], nums[4]]
     * = [0,1,2,4,5,3]
     * \
     * 输入：nums = [5,0,1,2,3,4]
     * 输出：[4,5,0,1,2,3]
     * 解释：数组 ans 构建如下：
     * ans = [nums[nums[0]], nums[nums[1]], nums[nums[2]], nums[nums[3]], nums[nums[4]], nums[nums[5]]]
     * = [nums[5], nums[0], nums[1], nums[2], nums[3], nums[4]]
     * = [4,5,0,1,2,3]
     * \
     *
     * @param args args
     */
    public static void main(String[] args) {
        BuildArray buildArray = new BuildArray();
        int[] nums = new int[]{0, 2, 1, 5, 3, 4};
        System.out.println(buildArray.buildArray(nums));
    }

    public int[] buildArray(int[] nums) {
        int[] ans = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            ans[i] = nums[nums[i]];
        }
        return ans;
    }
}
